package com.framework.sec.util.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.filter.OncePerRequestFilter;

import com.framework.bas.bs.IPlmBasBS;
import com.framework.bas.param.GeneralParam;
import com.framework.bas.service.IControl;
import com.framework.bas.util.PlmBasConstant;
import com.framework.sec.vo.LogonUserObject;
import com.framework.sog.vo.BasLogLogin;

public class SecBeforeLogoutFilter extends OncePerRequestFilter {
	
	private IPlmBasBS plmBasBS;
	private IControl plmBasControl;
	private String filterProcessesUrl = "/j_spring_security_logout";

	@Override
	protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
		
		if(requiresLogout(request, response)){
			//登出日志
			addLogoutLog(request);
		}
	}
	private void addLogoutLog(HttpServletRequest request) {
		HttpSession session = request.getSession(false);
    	if (session != null) {
			GeneralParam qp = GeneralParam.getCurrent();
			BasLogLogin logvo = new BasLogLogin();
			LogonUserObject appsLogonUserVO = (LogonUserObject)request.getSession().getAttribute(PlmBasConstant.SES_USER_LOGON_INFO);
			if(appsLogonUserVO==null){
				return;
			}
//			logvo.setUserCode(appsLogonUserVO.getUserCode());
//			logvo.setLoginId(appsLogonUserVO.getLoginId());
//			logvo.setUserName(appsLogonUserVO.getUserName());
//			logvo.setLogonDate(UtilDatetime.getDateString());
//			logvo.setLogonTime(UtilDatetime.getTimeString());
//			logvo.setLogonFlag(SogConstant.SOG_LOGON_OUT);
//			logvo.setLogonIp(request.getRemoteAddr());
//			logvo.setLogonLast("1");
//			qp.setValue(SogConstant.SERVICE_ID_SOG0002, logvo);
//			if (qp.getValueObject(ApiBasConstant.APP_USER) == null) {
//				qp.setValue(ApiBasConstant.APP_USER, appsLogonUserVO);
//			}
//			if (qp.getValueObject(ApiBasConstant.APP_SERVICE) == null) {
//				qp.setValue(ApiBasConstant.APP_SERVICE, SogConstant.SERVICE_ID_SOG0002);
//				HpBasServiceVO hpBasServiceVO = getBasBS().queryServiceByCode(SogConstant.SERVICE_ID_SOG0002);
//				qp.setValue(ApiBasConstant.APP_SERVICE_OBJECT, hpBasServiceVO);
//			}
//			basControl.execute(SogConstant.SERVICE_ID_SOG0002, qp);
    	}
	}
	protected boolean requiresLogout(HttpServletRequest request, HttpServletResponse response) {
        String uri = request.getRequestURI();
        int pathParamIndex = uri.indexOf(';');

        if (pathParamIndex > 0) {
            // strip everything from the first semi-colon
            uri = uri.substring(0, pathParamIndex);
        }

        int queryParamIndex = uri.indexOf('?');

        if (queryParamIndex > 0) {
            // strip everything from the first question mark
            uri = uri.substring(0, queryParamIndex);
        }

        if ("".equals(request.getContextPath())) {
            return uri.endsWith(filterProcessesUrl);
        }

        return uri.endsWith(request.getContextPath() + filterProcessesUrl);
    }
	public IPlmBasBS getPlmBasBS() {
		return plmBasBS;
	}
	public void setPlmBasBS(IPlmBasBS plmBasBS) {
		this.plmBasBS = plmBasBS;
	}
	public IControl getPlmBasControl() {
		return plmBasControl;
	}
	public void setPlmBasControl(IControl plmBasControl) {
		this.plmBasControl = plmBasControl;
	}
	public String getFilterProcessesUrl() {
		return filterProcessesUrl;
	}
	public void setFilterProcessesUrl(String filterProcessesUrl) {
		this.filterProcessesUrl = filterProcessesUrl;
	}

}
